<?php

namespace maoxuner\LaravelJsonRpc\Middlewares;

use Closure;
use Illuminate\Http\Request;
use maoxuner\LaravelJsonRpc\Exceptions\Handler;
use Symfony\Component\HttpFoundation\Response;

class JsonRpcMiddleware
{
    public function __construct(
        protected Handler $handler
    ) {}

    public function handle(Request $request, Closure $next): Response
    {
        $response = $next($request);

        return $response?->exception ? $this->handler->render($request, $response->exception) : $response;
    }
}
